//    Openbravo POS is a point of sales application designed for touch screens.
//    Copyright (C) 2007-2009 Openbravo, S.L.
//    http://www.openbravo.com/product/pos
//
//    This file is part of Openbravo POS.
//
//    Openbravo POS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Openbravo POS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Openbravo POS.  If not, see <http://www.gnu.org/licenses/>.
package com.openbravo.pos.mant;

import java.awt.Component;
import java.util.Properties;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.format.Formats;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.user.DirtyManager;
import cn.ekuma.data.ui.swing.AbstractDAOJEditor;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import com.openbravo.bean.Floor;
import com.openbravo.bean.Place;

/**
 *
 * @author adrianromero
 */
@SuppressWarnings("serial")
public class PlacesEditor extends AbstractDAOJEditor<Place> {
    private ComboBoxValModel m_FloorModel;
    I_DataLogicSales dlSales;

    /** Creates new form PlacesEditor */
    public PlacesEditor(I_DataLogicSales dlSales, DirtyManager dirty) {
    	super(dirty);
        initComponents();

        this.dlSales=dlSales;
        m_FloorModel = new ComboBoxValModel();

        m_jName.getDocument().addDocumentListener(dirty);
        m_jFloor.addActionListener(dirty);
        m_jX.getDocument().addDocumentListener(dirty);
        m_jY.getDocument().addDocumentListener(dirty);
        jm_sID.getDocument().addDocumentListener(dirty);
    }

    public void activate() throws BasicException {
        m_FloorModel = new ComboBoxValModel(dlSales.list(Floor.class));
        m_jFloor.setModel(m_FloorModel);
    }

    public void refresh() {
    }


    public Component getComponent() {
        return this;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        m_jName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        m_jX = new javax.swing.JTextField();
        m_jY = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        m_jFloor = new javax.swing.JComboBox();
        JLabel4 = new javax.swing.JLabel();
        jm_sID = new javax.swing.JTextField();

        setLayout(null);

        jLabel2.setText(AppLocal.getIntString("Label.Name")); // NOI18N
        add(jLabel2);
        jLabel2.setBounds(20, 40, 90, 15);
        add(m_jName);
        m_jName.setBounds(110, 40, 180, 23);

        jLabel3.setText(AppLocal.getIntString("label.placeposition")); // NOI18N
        add(jLabel3);
        jLabel3.setBounds(20, 100, 90, 15);
        add(m_jX);
        m_jX.setBounds(110, 100, 50, 23);
        add(m_jY);
        m_jY.setBounds(170, 100, 50, 23);

        jLabel1.setText(AppLocal.getIntString("label.placefloor")); // NOI18N
        add(jLabel1);
        jLabel1.setBounds(20, 70, 90, 15);
        add(m_jFloor);
        m_jFloor.setBounds(110, 70, 170, 20);

        JLabel4.setText("ID");
        add(JLabel4);
        JLabel4.setBounds(20, 10, 90, 15);

        jm_sID.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        add(jm_sID);
        jm_sID.setBounds(110, 10, 100, 23);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel JLabel4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jm_sID;
    private javax.swing.JComboBox m_jFloor;
    private javax.swing.JTextField m_jName;
    private javax.swing.JTextField m_jX;
    private javax.swing.JTextField m_jY;
    // End of variables declaration//GEN-END:variables

	@Override
	public Place createNew(Properties prop) {
		return new Place();
	}

	@Override
	public void rebuild(Place obj) throws BasicException {
		obj.setKey(jm_sID.getText());
        obj.setM_sName(m_jName.getText());
        obj.setM_ix((Integer)(Formats.INT.parseValue(m_jX.getText())));
        obj.setM_iy((Integer) Formats.INT.parseValue(m_jY.getText()));
        obj.setM_sfloor((String) m_FloorModel.getSelectedKey());
	}

	@Override
	public void updateUI(boolean editAble, int state) {
        jm_sID.setText(obj.getId());
        m_jName.setText(obj.getName());
        m_jX.setText(Formats.INT.formatValue(obj.getX()));
        m_jY.setText(Formats.INT.formatValue(obj.getY()));
        m_FloorModel.setSelectedKey(obj.getFloor());

        if(state!=STATE_Insert)
        	jm_sID.setEditable(true);
        else
           jm_sID.setEnabled(false);
        m_jName.setEnabled(editAble);
        m_jFloor.setEnabled(editAble);
        m_jX.setEnabled(editAble);
        m_jY.setEnabled(editAble);
		
	}

	@Override
	public Class getEditClass() {
		return Place.class;
	}
}
